﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT.BusinessLogic;
using TMDT.BusinessObjects;
using TMDT.Web.UI.Commons;

namespace TMDT.Web.UI.Controls
{
    public partial class LoaiSanPhamControl : System.Web.UI.UserControl
    {
        public static Guid IDLoaiSanPham;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            mtvView.ActiveViewIndex = 0;

            txtTenLoaiSanPhamInsert.Text = "";
            LoadChungLoai();
            LoadLoaiSanPham();
        }

        private void LoadLoaiSanPham()
        {
            var service = new LoaiSanPhamManager();
            rptLoaiSanPham.DataSource = service.GetLoaiSanPham();
            rptLoaiSanPham.DataBind();
        }

        private void LoadChungLoai()
        {
            var service = new ChungLoaiSanPhamManager();
            ddlIDChungLoaiInsert.LoadDataSource(service.GetChungLoaiSanPham(), "TenChungLoai", "IDChungLoai");
            ddlIDChungLoaiUpdate.LoadDataSource(service.GetChungLoaiSanPham(), "TenChungLoai", "IDChungLoai");
        }

        protected void lbnInsert_Click(object sender, EventArgs e)
        {
            var service = new LoaiSanPhamManager();
            var entity = new LoaiSanPham
                             {
                                 IDLoaiSanPham = Guid.NewGuid(),
                                 IDChungLoai = new Guid(ddlIDChungLoaiInsert.SelectedValue),
                                 TenLoaiSanPham = txtTenLoaiSanPhamInsert.Text
                             };
            service.Add(entity);

            LoadData();
        }

        protected void lbnUpdate_Click(object sender, EventArgs e)
        {
            var service = new LoaiSanPhamManager();
            var entity = service.GetLoaiSanPham(IDLoaiSanPham);

            entity.IDChungLoai = new Guid(ddlIDChungLoaiUpdate.SelectedValue);
            entity.TenLoaiSanPham = txtTenLoaiSanPhamUpdate.Text;

            service.Update(entity);
            LoadData();
        }

        protected void lbnCancel_Click(object sender, EventArgs e)
        {
            mtvView.ActiveViewIndex = 0;
        }

        protected void ibnDelete_Click(object sender, ImageClickEventArgs e)
        {
            var id = (((ImageButton)sender).CommandArgument);
            var service = new LoaiSanPhamManager();
            service.Remove(id);

            LoadData();
        }

        protected void ibnEdit_Click(object sender, ImageClickEventArgs e)
        {
            var id = ((ImageButton)sender).CommandArgument;
            var service = new LoaiSanPhamManager();
            var entity = service.GetLoaiSanPham(id);

            txtTenLoaiSanPhamUpdate.Text = entity.TenLoaiSanPham;
            ddlIDChungLoaiUpdate.SelectedValue = entity.IDChungLoai.ToString();
            IDLoaiSanPham = entity.IDLoaiSanPham;
            
            mtvView.ActiveViewIndex = 1;
        }

        protected void rptLoaiSanPham_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var entity = (LoaiSanPham) e.Item.DataItem;

            var lblChungLoai = e.Item.FindControl("lblChungLoai") as Label;
            var chungLoai = new ChungLoaiSanPhamManager();
            lblChungLoai.Text = chungLoai.GetChungLoaiSanPham(entity.IDChungLoai).TenChungLoai;
        }
    }
}